﻿using System;
using GameFramework;
using GameFramework.UI.FairyGUI;
using GameLogic.Launch;

namespace GameLogic.UI.Launch
{
    public partial class LaunchForm : AFairyGUIFormLogic
    {
        #region [界面设置]

        /// <summary>
        /// 界面分组设置
        /// </summary>
        protected override IFairyGUIFormGroupSetting FormGroupSetting => GameMainFormGroupSetting.That;

        /// <summary>
        /// 是否是全屏界面
        /// </summary>
        protected override bool IsFullScreen => true;

        /// <summary>
        /// 是否是水平居中
        /// </summary>
        protected override bool IsHorizontalCenter => true;

        /// <summary>
        /// 是否垂直居中
        /// </summary>
        protected override bool IsVerticalCenter => true;

        #endregion
        
        private void OnInitialize(object userData)
        {
            m_ProgressBar.min = 0;
            m_ProgressBar.max = (int)LaunchStep.LaunchComplete;
        }

        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);

            CurrentStateControllerPage = StateControllerPage.PageLaunch;
            m_ProgressBar.value = 0f;
            AddListenerDisposeOnClose<LaunchStepUpdatedEvent>(OnReceiveLaunchStepUpdatedEventArgs);
        }

        protected override void OnRecycle()
        {
        }

        private void OnReceiveLaunchStepUpdatedEventArgs(object sender, EventArgs e)
        {
            var eventArgs = (LaunchStepUpdatedEvent)e;
            var progress = (int)eventArgs.Step + eventArgs.Progress;
            m_ProgressBar.TweenValue(progress, 0.3f);

            if (eventArgs.Step + 1 >= LaunchStep.LaunchComplete)
                CurrentStateControllerPage = StateControllerPage.PageLogin;
        }
    }
}